package com.graplo.gwtgaefb.client;

import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.graplo.gwtgaefb.client.examples.CallFacebookClient;
import com.graplo.gwtgaefb.client.examples.GeneralRequestPanel;

public class GwtGaeFb implements EntryPoint {

	public void onModuleLoad() {
		final VerticalPanel mainPanel = new VerticalPanel();
		RootPanel.get().add(mainPanel);
		Image logo = new Image("wiki/gwt-gae-fb-logo.png");
		mainPanel.add(logo);
		mainPanel.add(createContinuousFbCheck(500, new Accomplished() {
			@Override
			public void accomplished(boolean success) {
				mainPanel.add(prepareRequestPanels());
			}
		}));
	}

	private Widget prepareRequestPanels() {
		TabPanel tabPanel = new TabPanel();
		String multifriendRequestCaption = "Request multifriend";
		tabPanel.add(createMultifriendRequestPanel(multifriendRequestCaption), multifriendRequestCaption);
		String feedDialogCaption = "Feed dialog";
		tabPanel.add(createFeedDialogPanel(feedDialogCaption), feedDialogCaption);
		String apiCallCaption = "FB API call";
		tabPanel.add(createApiCallPanel(apiCallCaption), apiCallCaption);
		tabPanel.selectTab(0);
		return tabPanel;
	}

	private Widget createMultifriendRequestPanel(String caption) {
		final String [] paramNames = {"message"};
		final String [] defaultValues = {"This app is great!"};
		GeneralRequestPanel requestPanel = new GeneralRequestPanel(caption, paramNames, defaultValues, new CallFacebookClient() {
			@Override
			public void call(Map<String, String> parameters, ResponseHandler responseHandler) {
				HandyFacebookClient.dialogRequestMultiFriend(parameters.get(paramNames[0]), responseHandler);
			}
		});
		return requestPanel;
	}

	private Widget createFeedDialogPanel(String caption) {
		final String [] paramNames = {"name", "caption", "description", "link", "picture"};
		final String [] defaultValues = {"GWT-GAE-FB", "Check out this app!", "GWT-GAE-FB shows how to integrate these three technologies.", "http://apps.facebook.com/gwtgaefb/", "http://gwtgaefb.appspot.com/gfx/hat.png"};
		GeneralRequestPanel requestPanel = new GeneralRequestPanel(caption, paramNames, defaultValues, new CallFacebookClient() {
			@Override
			public void call(Map<String, String> parameters, ResponseHandler responseHandler) {
				HandyFacebookClient.dialogFeed(parameters.get(paramNames[0]), parameters.get(paramNames[1]), parameters.get(paramNames[2]),
						parameters.get(paramNames[3]), parameters.get(paramNames[4]), responseHandler);
			}
		});
		return requestPanel;
	}

	private Widget createApiCallPanel(String caption) {
		final String [] paramNames = {"path"};
		final String [] defaultValues = {"/me"};
		GeneralRequestPanel requestPanel = new GeneralRequestPanel(caption, paramNames, defaultValues, new CallFacebookClient() {
			@Override
			public void call(Map<String, String> parameters, ResponseHandler responseHandler) {
				FacebookClient.api(parameters.get(paramNames[0]), responseHandler);
			}
		});
		return requestPanel;
	}

	private Widget createContinuousFbCheck(int milliseconds, final Accomplished accomplished) {
		final Label label = new Label("Checking FB");
		if (GWT.isProdMode()) {
			 Timer t = new Timer() {
			     public void run() {
			         if (JsUtil.checkFBready()) {
			        	 label.setText("FB available");
			        	 cancel();
			        	 accomplished.accomplished(true);
			         } else {
			        	 label.setText(label.getText() + ".");
			         }
			     }
			 };
			 t.scheduleRepeating(milliseconds);
		} else {
			label.setText("DEV MODE");
			accomplished.accomplished(true);
		}
		return label;
	}
}
